home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / mmdf / mmdf-IIb.43 / lib / util / equal.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-02-28  |  1.1 KB  |  51 lines

  1. #include "util.h"
  2.  
  3. /*
  4.  * equal(s1, s2, n)-- compare two strings to see if they're equiv, but
  5.  *            constrain ourselves to equal length strings.
  6.  *
  7.  * If both strings end at the same position and were equivalent for all
  8.  * previous characters, then return TRUE.
  9.  *
  10.  * If both strings are equivalent for their first n characters,
  11.  * then return TRUE.
  12.  *
  13.  * ELSE
  14.  *
  15.  * return FALSE.
  16.  *
  17.  * If n <= 0 then ignore the second condition above.
  18.  *
  19.  * IMPROVEMENT: sanity checks arguments to see if they're good strings.
  20.  *        stops at the end of either string
  21.  *        the n <= 0 thing is an added flexibility
  22.  */
  23. equal(str1, str2, n)
  24. register char *str1, *str2;
  25. int n;
  26. {
  27.     extern        char    chrcnv[];
  28.     register    int    cnt;
  29.  
  30.     if (str1 == 0 || str2 == 0)
  31.         return(str1 == str2);
  32.  
  33.     for (cnt = 1; chrcnv[*str1] == chrcnv[*str2]; str1++, str2++) {
  34.  
  35.         /*** A string ended. ***/
  36.         if ((*str1 == 0)||(*str2 == 0)) {
  37.             if ((*str1 == 0)&&(*str2 == 0))
  38.                 return(TRUE);
  39.             else
  40.                 return(FALSE);
  41.         }
  42.  
  43.         /*** Are they equiv for the first n(n>0)characters? ***/
  44.         cnt++;
  45.         if ((n > 0) && (cnt > n))
  46.             return(TRUE);
  47.     }
  48.  
  49.     return(FALSE);
  50. }
  51.